package com.softserve.academy.food.controllers;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.softserve.academy.food.model.mDish;
import com.softserve.academy.food.model.mUserCredentials;
import com.softserve.academy.food.model.mUserInfo;
import com.softserve.academy.food.services.iHistoryOrder;
import com.softserve.academy.food.services.iUserService;
import com.softserve.academy.food.services.iViewDish;
import com.softserve.academy.food.services.supplement.Convector;


@Controller
@RequestMapping( value="/index" )
public class ViewController  
{
	@Autowired
	private iUserService uService;
	@Autowired
	private iViewDish dService;
	@Autowired
	private iHistoryOrder hService;
	
	private ModelAndView model;
	
	@SuppressWarnings("unchecked")
	@RequestMapping( method = RequestMethod.GET )
	public ModelAndView mainView( HttpSession session )
	{	
		if ( dService.getDishs().isEmpty() )
		{
			dService.setAll();
		}
		
		model = new ModelAndView("index");
		model.addObject("listdishs", dService.getDishs());
		
		if ( session.getAttribute("baskedIdList")!=null )
		{
			model.addObject( "baskedNameList", (ArrayList<String>)session.getAttribute("baskedNameList") );
			model.addObject( "baskedSumm", (Float)session.getAttribute("baskedSumm") );
		}
		else
		{ 
			session.setAttribute( "baskedNameList", new  ArrayList<String>() );
			session.setAttribute( "baskedSumm", new Float(0) );
			session.setAttribute( "baskedIdList", new ArrayList<Integer>() );
		}
		
		return model;
	}
	
	@RequestMapping( params={"action=registration"}, method = RequestMethod.GET )
	public ModelAndView registrationView()
	{
		model = new ModelAndView("index");
		model.addObject( "listdishs", dService.getDishs() );
		model.addObject( "user", new mUserCredentials() );
		
		return model;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping( params={"action=commit"}, method = RequestMethod.GET )
	public ModelAndView comitView( HttpSession session )
	{
		float summ=0;
		model = new ModelAndView("index");
		
		ArrayList<Integer> baskedIdList = (ArrayList<Integer>)session.getAttribute( "baskedIdList" );
		ArrayList<mDish> listdishs = dService.getDishesByIds( baskedIdList );
		
		model.addObject( "listdishs", dService.getDishs() );
		model.addObject( "baskedDish", listdishs );
		
		for ( mDish dish : listdishs )
		{
			summ+=dish.getPrice();
		}
		
		model.addObject( "baskedSummPrice",  summ );

		return model;
	}
	
	@RequestMapping( params={"action=registration"}, method = RequestMethod.POST )
	public ModelAndView dataReques( @ModelAttribute("User") mUserCredentials user )
	{
		if ( uService.checkUser(user.getLogin()) )
		{
			model = new ModelAndView("index");
			model.addObject("listdishs", dService.getDishs());
			model.addObject("user", new mUserCredentials());
			model.addObject("checkLogin", "Логин занят");
			return model;
		}
		
		uService.addUser( user );
		
		model = new ModelAndView( "redirect:/index.htm?action=registred" );
		model.addObject( "listdishs", dService.getDishs() );
		model.addObject( "user", user );
		
		return model;
	}
	
	@RequestMapping( params={"action=setdishtype"}, method = RequestMethod.POST )
	public String setDishType( HttpServletRequest request )
	{
		dService.setType( Convector.StringToInt( request.getParameter("idtype") ) );
		
		return "redirect:/index.htm";
	}
	
	@RequestMapping( params={"action=viewuserinfo"}, method = RequestMethod.POST )
	public ModelAndView viewUserInfo( @ModelAttribute("mUserinfo") mUserInfo user )
	{
		model = new ModelAndView("index");
		model.addObject("listdishs", dService.getDishs());
		
		if (!getLogin().equals("guest"))
		{	
			model.addObject("userinfo", uService.getUser( getLogin() ));
			
			if ( user!=null & user.getId()>0 )
			{
				uService.updateUser( user );
			}
		}
		
		return model;
	}

	@RequestMapping( params={"action=viewhistory"}, method = RequestMethod.POST )
	public ModelAndView viewUserHistory()
	{
		model = new ModelAndView("index");
		model.addObject("listdishs", dService.getDishs());
		
		if (!getLogin().equals("guest"))
		{
			model.addObject("historylist", hService.getHistory( getLogin() ));
		}
		
		return model;
	}
	
	private String getLogin()
	{
		String login;
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if ( principal instanceof UserDetails ) 
		{
			login = ( (UserDetails)principal ).getUsername();
		} 
		else 
		{
			login = principal.toString();
		}
		return login;
	}
}

